<?php
/**
 * Created by mq.
 * User: mq
 * Date: 18-8-11
 * Time: 下午5:00
 */

namespace tool;

class auth
{

    static public function conf()
    {

        $arr = [
            0 => [
                'key' =>'admin/Index/index',
                'name' => '管理首页'
            ],
            100=>[
                'key' => 'admin/Index/goods_list',
                'name' => '商品列表'
            ],
            101=>[
                'key' => 'admin/Index/goods_add',
                'name' => '添加商品',
                'isSon' => 1
            ],
            102=>[
                'key' => 'admin/Index/goods_del',
                'name' => '删除商品',
                'isSon' => 1
            ],
            103=>[
                'key' => 'admin/Index/goods_edit',
                'name' => '修改商品',
                'isSon' => 1
            ],
            150=>[
                'key' => 'admin/Index/admin_list',
                'name' => '管理员列表'
            ],
            151=>[
                'key' => 'admin/Authpage/index',
                'name' => '权限修改',
                'isSon' => 1
            ],
            152=>[
                'key' => 'admin/Index/del_admin',
                'name' => '删除管理员',
                'isSon' => 1
            ],
            201 =>[
                'key' => 'admin/Index/config',
                'name' => '网站配置'
            ],
            202 => [
                'key' => 'admin/Index/add_music',
                'name' => '添加背景音乐',
                'isSon' => 1
            ],
            301 => [
                'key' => 'admin/index/update_password',
                'name' => '修改密码'
            ]
        ];

        $res = [];

        foreach ($arr as $key => $val) {
            $newKey = md5($arr[$key]['key']);
            $res[$newKey] = $arr[$key];
            $res[$newKey]['key'] = $key;
        }

        return $res;

    }

    static public function verdict($request, $userAuth){
        $module = $request->module();
        $contorller = $request->controller();
        $action = $request->action();

        $auth = self::conf();
        $key = md5($module.'/'.$contorller.'/'.$action);

        $res = isset($auth[$key]) && isset($auth[$key]['key'])&& in_array($auth[$key]['key'],$userAuth);

        if(isset($auth[$key]) && !$auth[$key]['key']) $res = true;

        return $res;
    }


}